package com.xzj.admin.resource.sync.controller;

import com.xzj.admin.resource.sync.domain.vo.SelectOption;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import com.xzj.common.id.gen.DefaultIdHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.resource.sync.domain.entity.ResFieldsManage;
import com.xzj.admin.resource.sync.service.ResFieldsManageService;

import java.util.List;

/**
 * @program: xzj-admin
 * @ClassName: ResFieldsManageController
 * @description: 资源字段管理;(res_fields_manage)表控制层
 * @author: XiaZunJun
 * @create: 2024-10-31
 */
@Tag(name = "资源字段管理对象功能接口")
@RestController
@RequestMapping("/resFieldsManage")
public class ResFieldsManageController{
    @Resource
    private ResFieldsManageService resFieldsManageService;

    /**
     * 通过ID查询单条数据
     *
     * @param fieldId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{fieldId}")
    public ResFieldsManage queryById(@PathVariable(name = "fieldId") String fieldId){
        return resFieldsManageService.queryById(fieldId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<ResFieldsManage> pageQuery(@RequestBody PageReq<ResFieldsManage> pageReq){
        return resFieldsManageService.pageQuery(pageReq);
    }

    /**
     * 新增数据
     *
     * @param resFieldsManage 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public ResFieldsManage addOrUpdate(@RequestBody ResFieldsManage resFieldsManage){
        if(ObjectUtil.isNotEmpty(resFieldsManage.getFieldId())){
            resFieldsManageService.update(resFieldsManage);
        }else{
            resFieldsManage.setFieldId(DefaultIdHelper.nextIdStr());
            resFieldsManageService.insert(resFieldsManage);
        }
        return resFieldsManage;
    }

    /**
     * 通过主键删除数据
     *
     * @param fieldId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{fieldId}")
    public boolean deleteById(@PathVariable(name = "fieldId") String fieldId){
        return resFieldsManageService.deleteById(fieldId);
    }

    @Operation(summary ="通过资源ID查询数据")
    @GetMapping("/queryByResId/{resId}")
    public List<SelectOption> queryByResId(@PathVariable(name = "resId") String resId){
        return resFieldsManageService.queryByResId(resId);
    }
}